﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RayTracerLib.Engine;
using CommonLib.Common;
using System.IO;
using CommonLib;
using CommonLib.Geometry;
using Microsoft.DirectX;
namespace Editor
{
    public partial class PanelSettings : UserControl
    {
        public event EventHandler RefreshImage;
        public PanelSettings()
        {
            InitializeComponent();
        }
        private void LoadSettings()
        {
            LoadPlugins();
        }
        private void LoadPlugins()
        {
            int i = 0;
            if (PluginManager.Current == null)
                return;
            if (cmb_plugins.Items.Count == 0)
                foreach (var plugin in PluginManager.Current.Plugins)
                {
                    cmb_plugins.Items.Add(new Pair(plugin.ID, plugin.Description));
                    if (plugin.ID == Properties.Settings.Default.ShadingID)
                        i = cmb_plugins.Items.Count - 1;
                }
            if (string.IsNullOrEmpty(Properties.Settings.Default.ShadingID) && cmb_plugins.Items.Count > 0)
            {
                Properties.Settings.Default.ShadingID = ((Pair)cmb_plugins.Items[0]).Key;
                Properties.Settings.Default.Save();
            }
            else
                if (cmb_plugins.Items.Count > 0)
                    cmb_plugins.SelectedIndex = i;
        }
        private void SavePlugins()
        {
            if (cmb_plugins.SelectedItem != null)
            {
                Properties.Settings.Default.ShadingID = (cmb_plugins.SelectedItem as Pair).Key;
                Properties.Settings.Default.Save();
            }
        }
        private void SaveSettings()
        {
            SavePlugins();
        }
        public Settings GetCurrentSettings()
        {
            var settings = new Settings();
            IPluginShading plugin = null;
            if (cmb_plugins.SelectedItem as Pair != null)
                plugin = PluginManager.Current.GetPluginByID((cmb_plugins.SelectedItem as Pair).Key);
            if (plugin == null)
            {
                settings.ShadingClass = PluginManager.Current.GetAnyPlugin();
                if (settings.ShadingClass != null)
                {
                    Properties.Settings.Default.ShadingID = settings.ShadingClass.ID;
                    Properties.Settings.Default.Save();
                }
            }
            else
                settings.ShadingClass = plugin;
            return settings;
        }
        public Camera GetCurrentCamera()
        {
            return new Camera(new Point3D(0, 0, 25), new Vector3(0, 0, -1), 100, 100, 50);
        }
        protected void OnRefreshImage(EventArgs e)
        {
            if (RefreshImage != null)
                RefreshImage(this, e);
        }
        private void btn_render_Click(object sender, EventArgs e)
        {
            OnRefreshImage(EventArgs.Empty);
        }

        private void FlowLayoutPanelMainSizeChanged(object sender, EventArgs e)
        {
            foreach (Control c in flowLayoutPanel_main.Controls)
                c.Width = flowLayoutPanel_main.Width;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            SaveSettings();
        }

        private void PanelSettings_Load(object sender, EventArgs e)
        {
            LoadSettings();
        }

    }
}
